package com.yanqu.road.server.manager.activity.tongbing;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.activity.tongbing.activity.CrossTongBingActivity;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingMgr extends TempMgr {

    public final static Map<Integer, Map<Integer, CrossTongBingActivity>> activityGroupMap = new ConcurrentHashMap<>();

    public static synchronized CrossTongBingActivity getActivity(int activityId, int groupId) {
        // 【活动ID判断】
        if (activityGroupMap.get(activityId) == null) {
            activityGroupMap.put(activityId, new ConcurrentHashMap<>());
        }
        Map<Integer, CrossTongBingActivity> groupMap = activityGroupMap.get(activityId);

        // 【分组ID判断】
        UnionActivityGroup groupData = Cross2UnionActivityGroupMgr.getGroupData(activityId, groupId);
        if (groupData == null) {
            return null;
        }
        if (groupMap.get(groupId) == null) {
            ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
            if (activityInfo == null) {
                return null;
            }
            CrossTongBingActivity activity = new CrossTongBingActivity(activityInfo, groupId);
            groupMap.put(groupId, activity);
        }
        CrossTongBingActivity activity = groupMap.get(groupId);
        return activity;
    }

    public static Map<Integer, Map<Integer, CrossTongBingActivity>> getActivityGroupMap() {
        return activityGroupMap;
    }

    @Override
    public boolean save() {
        boolean logNeedSave = false;
        for (Map<Integer, CrossTongBingActivity> map : activityGroupMap.values()) {
            if (map.size() > 0) {
                logNeedSave = true;
                break;
            }
        }
        if (logNeedSave) {
            getLogger().info("统兵演武保存中，不要关服...");
        }

        for (Map<Integer, CrossTongBingActivity> map : activityGroupMap.values()) {
            for (CrossTongBingActivity activity : map.values()) {
                activity.save();
            }
        }
        if (logNeedSave) {
            getLogger().info("统兵演武保存完成");
        }

        // 移除旧活动
        for (Map.Entry<Integer, Map<Integer, CrossTongBingActivity>> entry : activityGroupMap.entrySet()) {
            int activityId = entry.getKey();
            for (Map.Entry<Integer, CrossTongBingActivity> entry2 : entry.getValue().entrySet()) {
                int groupId = entry2.getKey();
                CrossTongBingActivity activity = entry2.getValue();
                long canRemoveTime = activity.getConfig().getActivityInfo().getEndShowTime() * 1000 + DateHelper.HOUR_MILLIONS * 12;
                if (System.currentTimeMillis() > canRemoveTime) {
                    activity.setStop(true);
                    activityGroupMap.get(activityId).remove(groupId);
                    getLogger().info("移除统兵旧活动 {} 分组 {}", activityId, groupId);
                }
            }
        }

        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().error("tongbing reload config begin...");
        long now = System.currentTimeMillis() / 1000;
        for (Map<Integer, CrossTongBingActivity> map : activityGroupMap.values()) {
            for (CrossTongBingActivity activity : map.values()) {
                ActivityInfo activityInfo = activity.getConfig().getActivityInfo();
                if (now < activityInfo.getEndShowTime()) {
                    getLogger().error("tongbing reload config activity {} groupId {}", activity.getConfig().getActivityInfo().getActivityId(), activity.getGroupId());
                    activity.reloadConfig();
                }
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

}
